{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.747688Z",
     "start_time": "2025-03-28T14:01:37.050724Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import torch"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.778688Z",
     "start_time": "2025-03-28T14:01:38.752688Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.tensor([[3, 1, 4], [1, 5, 9], [2, 6, 5]])\n",
    "print(x)\n",
    "\n",
    "values, indices = torch.topk(x, k=2, dim=1)\n",
    "print(values, indices)\n",
    "\n",
    "values, indices = torch.topk(x, k=2, dim=1)\n",
    "print(values, indices)"
   ],
   "id": "c27ff5bb5a12aa57",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3, 1, 4],\n",
      "        [1, 5, 9],\n",
      "        [2, 6, 5]])\n",
      "tensor([[4, 3],\n",
      "        [9, 5],\n",
      "        [6, 5]]) tensor([[2, 0],\n",
      "        [2, 1],\n",
      "        [1, 2]])\n",
      "tensor([[4, 3],\n",
      "        [9, 5],\n",
      "        [6, 5]]) tensor([[2, 0],\n",
      "        [2, 1],\n",
      "        [1, 2]])\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.841360Z",
     "start_time": "2025-03-28T14:01:38.806694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "indices = torch.tensor([[0, 2], [1, 2], [1, 2]])\n",
    "scattered = torch.zeros([3, 3]).scatter_(1, indices, True)\n",
    "print(scattered)"
   ],
   "id": "9117793189e00cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0., 1.],\n",
      "        [0., 1., 1.],\n",
      "        [0., 1., 1.]])\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.873215Z",
     "start_time": "2025-03-28T14:01:38.859215Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.tensor([[1, 2], [3, 4]])\n",
    "print(x.shape)\n",
    "\n",
    "y = torch.unsqueeze(x, dim=0)\n",
    "print(y.shape)\n",
    "\n",
    "z = torch.unsqueeze(x, dim=1)\n",
    "print(z.shape)\n",
    "\n",
    "w = torch.unsqueeze(x, dim=2)\n",
    "print(w.shape)"
   ],
   "id": "4fac125b603c1e00",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 2])\n",
      "torch.Size([1, 2, 2])\n",
      "torch.Size([2, 1, 2])\n",
      "torch.Size([2, 2, 1])\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.903214Z",
     "start_time": "2025-03-28T14:01:38.889217Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.tensor([[3, 1, 4], [1, 5, 9], [2, 6, 5]])\n",
    "index_tensor = torch.tensor([[0, 2], [1, 2], [1, 2]])\n",
    "v = torch.gather(x, dim=1, index=index_tensor)\n",
    "print(v)"
   ],
   "id": "94bc99f75f00751",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3, 4],\n",
      "        [5, 9],\n",
      "        [6, 5]])\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T08:08:26.329168Z",
     "start_time": "2025-03-29T08:08:26.308167Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = torch.randn(2, 3)\n",
    "z = torch.randn(2, 3)\n",
    "print(y)\n",
    "print(z)\n",
    "result = (y + z)\n",
    "print(result)\n",
    "\n",
    "# 想要将结果重塑为特定形状，比如 (3, 2)\n",
    "new_shape = (3, 2)\n",
    "reshaped_result = result.view(new_shape)\n",
    "print(reshaped_result)"
   ],
   "id": "b1a8babf851521e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.9807, -1.3915,  1.4758],\n",
      "        [ 1.6226, -1.6214,  0.4703]])\n",
      "tensor([[ 0.3831,  0.0405, -0.8307],\n",
      "        [ 0.0957, -1.7703, -1.0669]])\n",
      "tensor([[ 1.3638, -1.3510,  0.6451],\n",
      "        [ 1.7183, -3.3917, -0.5966]])\n",
      "tensor([[ 1.3638, -1.3510],\n",
      "        [ 0.6451,  1.7183],\n",
      "        [-3.3917, -0.5966]])\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T14:01:38.967215Z",
     "start_time": "2025-03-28T14:01:38.952216Z"
    }
   },
   "cell_type": "code",
   "source": [
    "var = \"ceshi \"\n",
    "print(f\"\"\"{var}\"\"\")"
   ],
   "id": "9e6a9f39a3563c43",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceshi \n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T08:08:17.971782Z",
     "start_time": "2025-03-29T08:08:17.962782Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "print(np.dot(a,b))\n",
    "\n",
    "print(np.matmul(a,b))\n",
    "\n",
    "print(b - 0.1 * a)\n"
   ],
   "id": "64011fd20e678ac6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n",
      "32\n",
      "[3.9 4.8 5.7]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T08:08:14.344651Z",
     "start_time": "2025-03-29T08:08:14.329651Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])\n",
    "a.shape\n",
    "a[-2]"
   ],
   "id": "a9a0af9779596755",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 8, 9])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T05:33:46.098965Z",
     "start_time": "2025-03-29T05:33:46.084375Z"
    }
   },
   "cell_type": "code",
   "source": [
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "print(device)"
   ],
   "id": "bbb1472a5c7af3dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpu\n"
     ]
    }
   ],
   "execution_count": 16
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
